/**
 *
 */

var app = angular.module('zhijiaoAdminApp', ['oc.lazyLoad', 'ui.router', 'ngDialog', 'zhijiaoAdminApp.controllers', 'zhijiaoAdminApp.directives', 'zhijiaoAdminApp.services', 'zhijiaoAdminApp.filters']);
var nowTimes = new Date().getTime();
app.config(['$stateProvider', '$urlRouterProvider',
    function ($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('template', {
                url: '/:controller/:action/:query',
                templateUrl: function (e) {
                    return window._host + '/admin/' + e.controller + '/' + e.action + '?' + nowTimes;
                },
                resolve: loadJs()
            });
        $urlRouterProvider.otherwise('/index/home/');
    }
])


app.run(['$rootScope', '$state', '$location', '$timeout',
    function ($rootScope, $state, $location, $timeout) {
        $rootScope.location = function (path) {
            $timeout(function () {
                $location.path(path);
            }, 100);
        }

        $rootScope.$on('$stateChangeStart', function () {
            //loadingService.hide();
        });
        $rootScope.$on('$stateChangeSuccess', function () {
        })

    }
])

var loadJs = function () {
    return {
        load: ['$ocLazyLoad', '$stateParams', function ($ocLazyLoad, $stateParams) {
            var params = $stateParams;
            var filePath = [window._host + '/static/admin/js/controller/' + params.controller + '.js?' + nowTimes];
            var extras = (params.controller + '/' + params.action).toLocaleLowerCase();
            console.log(extras);
            if (actionExtraJs[extras]) {
                actionExtraJs[extras].forEach(function (v) {
                    if (JS_MAP[v]) {
                        var extra = JS_MAP[v];
                        if (angular.isArray(extra)) {
                            extra.forEach(function (e) {
                                filePath.push(e);
                            })
                        } else {
                            filePath.push(extra);
                        }
                    } else {
                        filePath.push(v);
                    }
                })
            }
            return $ocLazyLoad.load({
                files: filePath
            })
        }]
    };
}

var actionExtraJs = {
    'admin/index': ['jquery.MultifunctionImageManage'],
    'project/index': ['uploadoss'],
    'banner/index': ['uploadoss'],
};

var JS_MAP = {

    "jquery.MultifunctionImageManage": [
        window._host + "/static/public/js/jquery.MultifunctionImageManage/1.0.0/jquery.MultifunctionImageManage.css",
        window._host + "/static/public/js/jquery.MultifunctionImageManage/1.0.0/jquery.MultifunctionImageManage.js"
    ],

    'uploadoss': [
        window._host + '/static/admin/js/plupload.full.min.js',
        window._host + '/static/admin/js/upload.js'
    ],

};